home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug236 / xc2.c < prev    next >
Text File  |  1980-01-02  |  29KB  |  1,040 lines

  1. /*
  2.     HEADER:        CUG000.00;
  3.     TITLE:        Cross Reference Generator;
  4.     DATE:        04/27/1988;
  5.     DESCRIPTION:    "C-language program cross-referencer, modified for
  6.                 Microsoft C5.0, with enhancements."
  7.     VERSION:    1.1;
  8.     KEYWORDS:    Cross Reference;
  9.     FILENAME:    XC2.C;
  10.     SEE-ALSO:    CUG126, CUG171;
  11.     COMPILERS:    Vanilla;
  12.     AUTHORS:    Phillip N. Hisley, David N. Smith, Fred C. Smith,
  13.             William C. Colley III;
  14. */
  15. /**********************************************************
  16.  
  17.     XC - A 'C' Concordance Utility
  18.  
  19.     Version 1.0 January, 1982
  20.  
  21.     Copyright (c) 1982 by Philip N. Hisley
  22.  
  23.         Philip N. Hisley
  24.         548H Jamestown Court
  25.         Edgewood, Maryland 21040
  26.         (301) 679-4606
  27.  
  28.     Released for non-commercial distribution only
  29.  
  30.  
  31.     Converted to IBM/PC CI/C86 by David N. Smith, May/June 1983
  32.     with enhancements and Lattice compiler support in December 1983.
  33.  
  34.         David N. Smith
  35.         44 Ole Musket Lane
  36.         Danbury, CT 06810
  37.         (203) 748-5934
  38.  
  39.     Changes Copyright (c) 1983 by David N. Smith
  40.  
  41.     PC Enhancements include:
  42.  
  43.         1)    Nested #INCLUDE statements
  44.         2)    Single spaced cross-reference list
  45.         3)    Removal of tabbing on output device
  46.         (Since many printers don't support it)
  47.         4)    #INCLUDE statements with both "--" and <-->
  48.         syntax and with a full fileid in the quotes.
  49.         5)    Multiple input filenames on command line.
  50.  
  51.  
  52.     Converted to Microsoft C V5.0 with enhancements.
  53.     November/December 1987.
  54.  
  55.         Fred C. Smith
  56.         20 Whipple Ave.
  57.         Stoneham, MA 02180
  58.  
  59.     Enhancements/modifications include:
  60.  
  61.     1)  Call to reargv() at beginning of main(). If you are running
  62.         Allen Holub's Unix-like shell, reargv() will rebuild argc/argv
  63.         to contain the 2kbyte command-line which the shell provides.
  64.         If not using that shell, reargv is a no-op. (That shell is
  65.         available from M&T Books for around $40, including source!)
  66.     2)  Modify the include file lookups to use the same algorithm as
  67.         used by the Microsoft C compiler, i.e., look first in the
  68.         current directory for quote-delimited files. If a complete
  69.         pathname is specified look there only then quit. Any remaining
  70.         includes, including '<' or '"' delimited ones are then
  71.         searched for in the place(s) (if any) specified in the
  72.         optional parameter to the -i switch. If not found there,
  73.         searches the pathname(s) specified in the INCLUDE environment
  74.         variable.
  75.     3)  Modify the -i switch so that an optional trailing argument
  76.         specifies an alternate path for the includes, as in the -I
  77.         switch in the Microsoft C compiler's "cl" command. Multiple
  78.         -i switches may be given, each with a single pathname. These
  79.         multiple pathnames are saved in the order given.
  80.     4)  Make the default behavior to assume that comments are NOT
  81.         nested. Some PC-based C compilers (e.g. Lattice) support
  82.         nested comments, but Microsoft's does not. Also, those on
  83.         Unix and other larger systems do not. The ANSI standard
  84.         does not. For those who prefer to have support for nested
  85.         comments, a new command-line switch has been added so that
  86.         you can (optionally) have your cake, and eat it, too!
  87.     5)  Add recognition of keywords recognized in ANSI C, i.e.,
  88.         const, enum, signed, volatile, void. Remove recognition of
  89.         entry as a keyword (not in ANSI C). Also, add optional
  90.         recognition of Microsoft-specific keywords: cdecl, far,
  91.         fortran, huge, near and pascal. Use the -m switch for this
  92.         last group.
  93.     6)  Fix the awful kludge which previously existed in xc, in
  94.         which struct rf_blk.ref_cnt was declared as an int, but
  95.         under certain circumstances was used to hold a pointer to
  96.         a struct rf_blk. Replaced the declaration of ref_cnt as
  97.         an int with an appropriately declared union of the two
  98.         types needed.
  99.     7)  Minor tweaks to proc_file to fix incorrect line numbers
  100.         being generated around included files.
  101.     8)  In fil_chr(), used ferror() and feof() to implement the
  102.         original error-checking logic which had been commented
  103.         out in previous versions of the code.
  104.  
  105.     Version 1.1 -- April 1988:
  106.  
  107.     Microsoft-isms removed by William C. Colley, III.  In particular
  108.     the function access() is not available in many environments.  A
  109.     reorganization of the code eliminates the use of this function.     Note,
  110.     however, that the function open_include_file() does chop on file names
  111.     which is an inherently non-portable operation, so that function will
  112.     have to be cut to fit different operating systems' file naming
  113.     conventions.  Also, the call to getenv() may have to be modified or
  114.     removed.
  115.  
  116.     Also, added the -x option to remove the directories specified in the
  117.     INCLUDE environment variable from the search path for #include files.
  118.     WCC3.
  119.  
  120.     Abstract:
  121.  
  122.     'XC' is a cross-reference utility for 'C' programs.  Its has the
  123.     ability to handle nested include files to a depth of 8 levels and
  124.     properly processes nested comments as supported by BDS C. Option flags
  125.     support the following features:
  126.  
  127.         - Routing of list output to disk
  128.         - Cross-referencing of reserved words
  129.         - Processing of nested include files
  130.         - Generation of listing only
  131.  
  132.     Usage: xc <filename> <flag(s)>
  133.  
  134.     Flags:    -i [pathname]      = Enable file inclusion
  135.         -l          = Generate listing only
  136.         -m          = recognize Microsoft-specific keywords
  137.         -n          = Allow nested comments
  138.         -o <filename>      = Write output to named file
  139.         -r          = Cross-ref reserved words
  140.         -w <width>      = Maximum listing width
  141.         -x          = Exclude default include file dir's
  142.  
  143.     Note:    For the -i option, the pathname is optional.  The -i flag may
  144.         be given multiple times, each time with a pathname. These
  145.         pathnames are saved, building a list of places to search for
  146.         include files.    If no list is given the standard places will
  147.         be used.  If the -i option flag is not used, includes will not
  148.         be cross-referenced.
  149.  
  150. ***********************************************************/
  151.  
  152. /* Compiler specific stuff */
  153.  
  154. #define LINT_ARGS
  155. #include <stdio.h>
  156. #include <stdlib.h>
  157. #include <ctype.h>
  158. #include <malloc.h>
  159. #include <string.h>
  160. #undef LINT_ARGS
  161.  
  162. /* end compiler specific section */
  163.  
  164. /* Function declarations for xc's routines. */
  165.  
  166. #include "xc.h"
  167.  
  168. #ifndef TRUE
  169. #define TRUE        1
  170. #define FALSE        0
  171. #endif
  172.  
  173. #define DUMMY        0        /* a dummy integer value */
  174. #define ERROR        -1
  175. #define MAX_REF        5        /* maximum refs per ref-block */
  176. #define MAX_LEN        20        /* maximum identifier length */
  177. #define MAX_WRD        749        /* maximum number of identifiers */
  178. #define MAX_ALPHA   53        /* maximum alpha chain heads */
  179. #define REFS_PER_LINE 10    /* maximum refs per line */
  180. #define LINES_PER_PAGE 60
  181. #define MAXCOL        78        /* default max column number for listing line */
  182. #define MINCOL        30        /* minimum value for -w option */
  183. #define PATHLEN        128        /* maximum pathname length in chars. */
  184. #define FF        0x0C    /* formfeed */
  185.  
  186. typedef union {
  187.     int cnt;
  188.     struct rf_blk *pnext;
  189. } cnt;
  190.  
  191. struct rf_blk {
  192.     int     ref_item[MAX_REF];
  193.     cnt ref_cnt;
  194. } onerf;
  195.  
  196. struct id_blk {
  197.     char id_name[MAX_LEN];
  198.     struct id_blk *alpha_lnk;
  199.     struct rf_blk *top_lnk;
  200.     struct rf_blk *lst_lnk;
  201. } oneid;
  202.  
  203. struct id_blk *id_vector[MAX_WRD];
  204.  
  205. struct alpha_hdr {
  206.     struct id_blk *alpha_top;
  207.     struct id_blk *alpha_lst;
  208. };
  209.  
  210. struct alpha_hdr alpha_vector[MAX_ALPHA];
  211.  
  212. int linum;        /* line number */
  213. int edtnum = 0;        /* edit line number */
  214. int fil_cnt = 0;    /* active file index */
  215. int wrd_cnt = 0;    /* token count */
  216. int pagno = 0;        /* page number */
  217. int id_cnt = 0;        /* number of unique identifiers */
  218. int rhsh_cnt = 0;    /* number of conflict hits */
  219. int filevl = 0;        /* file level  */
  220. int paglin = 0;        /* page line counter */
  221. int maxcol = MAXCOL;    /* maximum right column for listing line */
  222. int prt_ref = FALSE;
  223. char act_fil[PATHLEN];
  224. char lst_fil[PATHLEN];
  225. char gbl_fil[PATHLEN];
  226. char i_path[PATHLEN];
  227. FILE *f_lst_fil;
  228. int n_flg = FALSE;
  229. int i_flg = FALSE;
  230. int o_flg = FALSE;
  231. int r_flg = FALSE;
  232. int l_flg = FALSE;
  233. int m_flg = FALSE;
  234. int x_flg = FALSE;
  235.  
  236.  
  237. /*************************************************************************/
  238.  
  239. int main(p_argc, p_argv)
  240. int p_argc;
  241. char **p_argv;
  242. {
  243.     char *arg, **argv;
  244.     int argc, i;
  245.     FILE *f;
  246.  
  247. #ifdef REARGV
  248.     reargv (&p_argc, &p_argv);
  249. #endif
  250.  
  251.     argc = p_argc;  argv = p_argv;
  252.     if (argc < 2) use_err();
  253.     while (--argc) {
  254.     if (*(arg = *++argv) == '-') {
  255.         switch (*++arg) {
  256.         case 'i':
  257.         case 'I':
  258.             i_flg = TRUE;
  259.             if (argc > 1 && **(argv + 1) != '-' && **(argv + 1)) {
  260.             if (strlen(i_path)) {
  261.                 strcat(i_path,";");     strcat(i_path,*++argv);
  262.             }
  263.             else strcpy(i_path,*++argv);
  264.             }
  265.             else i_path[0] = '\0';
  266.             if (i_path[0] == '-') use_err();
  267.             break;
  268.         case 'r':
  269.         case 'R':
  270.             r_flg++;
  271.             break;
  272.         case 'l':
  273.         case 'L':
  274.             l_flg++;
  275.             break;
  276.         case 'n':
  277.         case 'N':
  278.             n_flg++;
  279.             break;
  280.         case 'm':
  281.         case 'M':
  282.             m_flg++;
  283.             break;
  284.         case 'o':
  285.         case 'O':
  286.             o_flg++;
  287.             if (!--argc) use_err();
  288.             strcpy(lst_fil,*++argv);
  289.             if (lst_fil[0] == '-') use_err();
  290.             break;
  291.         case 'w':
  292.         case 'W':
  293.             if (!--argc) use_err();
  294.             if ((i = atoi(*++argv)) <= MINCOL || i >= 255) use_err();
  295.             maxcol = i;
  296.             break;
  297.         case 'x':
  298.         case 'X':
  299.             x_flg++;
  300.             break;
  301.         default:
  302.             use_err();
  303.             break;
  304.         }
  305.     }
  306.     }
  307.     if (o_flg) {
  308.     if (!(f_lst_fil = fopen(lst_fil,"w"))) {
  309.         printf("ERROR: Unable to create list file - %s\n",lst_fil);
  310.         exit(0);
  311.     }
  312.     printf("XC ... 'C' Concordance Utility  v1.0\n\n");
  313.     }
  314.     for (linum = 0;  linum < MAX_WRD;  ++linum) id_vector[linum] = NULL;
  315.     for (linum = 0; linum < MAX_ALPHA; ++linum)
  316.     alpha_vector[linum].alpha_top = alpha_vector[linum].alpha_lst = NULL;
  317.     linum = 0;
  318.     while (--p_argc && **++p_argv != '-') {
  319.     strcpy(gbl_fil,*p_argv);  strcpy(act_fil,*p_argv);
  320.     if (!(f = fopen(gbl_fil,"r")))
  321.         printf("\nERROR: Unable to open input file: %s\n",gbl_fil);
  322.     else proc_file(f,DUMMY);
  323.     }
  324.     if (!l_flg) {
  325.     gbl_fil[0] = '\0';  prnt_tbl();
  326.     printf("\nAllowable Symbols: %d\n",MAX_WRD);
  327.     printf("   Unique Symbols: %d\n",id_cnt);
  328.     }
  329.     if (o_flg) {
  330.     nl();  fclose(f_lst_fil);
  331.     }
  332.     return 0;
  333. }
  334.  
  335. /*************************************************************************/
  336.  
  337. void lst_err() {
  338.     printf("\nERROR: Write error on list output file - %s\n", lst_fil);
  339.     exit(0);
  340. }
  341.  
  342. /*************************************************************************/
  343.  
  344. void use_err() {
  345.     printf("\nERROR: Invalid parameter specification\n\n");
  346.     printf("Usage: xc <filename>... <flag(s)>\n\n");
  347.     printf("Flags: -i <opt. path(s)> = enable file inclusion\n");
  348.     printf("       -l                = Generate listing only\n");
  349.     printf("       -m                = Recognize Microsoft specific keywords\n");
  350.     printf("       -n                = Allow nested comments\n");
  351.     printf("       -o <outfile>      = Write output to named file\n");
  352.     printf("       -r                = Cross-reference reserved words\n");
  353.     printf("       -w width          = Width of output page; default=78\n");
  354.     printf("       -x                = Exclude default include file dir's\n");
  355.     printf("Flags must follow all input file names");
  356.     exit(0);
  357. }
  358.  
  359. /*************************************************************************/
  360.  
  361. int proc_file(infile,incnum)
  362. FILE *infile;
  363. int incnum;        /* prev. included line number (return to caller) */
  364. {
  365.     char token[MAX_LEN];    /* token buffer */
  366.     char cur_fil[PATHLEN];  /* name of file we are currently processing */
  367.     int eof_flg;        /* end-of-file indicator */
  368.     int tok_len;        /* token length */
  369.     FILE *f, *get_include_file();
  370.  
  371.     edtnum=0;
  372.     if (!filevl++) prt_hdr();
  373.     else nl();
  374.     eof_flg = FALSE;
  375.     do {
  376.     if (get_token(infile,token,&tok_len,&eof_flg,0)) {
  377.         if (chk_token(token)) {
  378.         if(!strcmp(token,"#include")) {
  379.             strcpy(cur_fil,act_fil);
  380.             if (f = get_include_file(infile))
  381.             edtnum = proc_file(f,edtnum);
  382.             else if (!i_flg)
  383.             printf("\nERROR: Unable to open input file: %s\n",
  384.                 act_fil);
  385.             strcpy(act_fil,cur_fil);
  386.         }
  387.         else put_token(token,linum);
  388.         }
  389.     }
  390.     } while (!eof_flg);
  391.  
  392.     --filevl;  fclose(infile);    return incnum;
  393. }
  394.  
  395. /*****************************************************************************
  396.  *  Reads the include filename from the source file and saves it into a global
  397.  *  variable, act_fil[].  It is assumed that the include statement gives the
  398.  *  filename only, without any drive specifier or path, as in the previous
  399.  *  version of XC.  Calls open_include_file() to flesh out the filename into a
  400.  *  full pathname and actually get the file opened.
  401.  */
  402.  
  403. FILE *get_include_file(infile)
  404. FILE *infile;
  405. {
  406.     char c, term, *p;
  407.     FILE *open_include_file();
  408.  
  409.     p = act_fil;
  410.  
  411.     while ((term = getc(infile)) == ' ') echo(term);
  412.     echo(term);
  413.     if (term == '<') term = '>';    /* terminator is > or " */
  414.     if ((term != '>') && (term != '"')) {
  415.     printf("Error scanning #INCLUDE fileid: %c\n",term);
  416.     exit(1);
  417.     }
  418.  
  419.     do {
  420.     if ((c = getc(infile)) != ' ') *p++ = c;
  421.     echo(c);
  422.     } while (c != term);
  423.     *--p = '\0';
  424.  
  425.     if (i_flg) return open_include_file(act_fil,term);
  426.     else return NULL;
  427. }
  428.  
  429. /*************************************************************************/
  430.  
  431. FILE *open_include_file(filenam,delim)
  432. char *filenam, delim;
  433. {
  434.     char fullnam[PATHLEN];
  435.     char includevar[4 * PATHLEN];
  436.     char *temp1, *temp2;
  437.     int include_len;
  438.     FILE *f;
  439.  
  440. /*
  441.  *  First, if delimited by quotes and file exists in current directory, return
  442.  *  a file handle for the opened file.    Next, if a complete pathname
  443.  *  specified, return a file handle for the opened file or NULL if the file
  444.  *  cannot be found.
  445.  */
  446.  
  447.     if (delim == '"' && (f = fopen(filenam,"r"))) return f;
  448.     if (filenam[1] == ':' || filenam[0] == '\\' || filenam[0] == '/')
  449.     return fopen(filenam,"r");
  450.  
  451. /*
  452.  *  Next, if a non-null pathname was provided with the -i switch, check in
  453.  *  that place.     Finally, check in the places specified by the INCLUDE
  454.  *  environment variable unless the -x switch was used.
  455.  */
  456.  
  457.     if (i_path[0] || (temp1 = getenv("INCLUDE"))) {
  458.     strcpy(includevar,";");
  459.     if (i_path[0]) { strcat(includevar,i_path);  strcat(includevar,";"); }
  460.     if (!x_flg) strcat(includevar,temp1);
  461.     include_len = strlen(temp2 = includevar);
  462.  
  463. /*
  464.  *  Break up the semicolon-delimited list of directories into
  465.  *  null-terminated strings and check each of them.
  466.  */
  467.  
  468.     while (include_len > 0) {
  469.         for (temp1 = temp2;     *temp2 != ';' && *temp2;  ++temp2)
  470.         --include_len;
  471.         if (*temp2 == ';') { *temp2++ = '\0';  --include_len; }
  472.         strcpy(fullnam,temp1);  strcat(fullnam,"\\");
  473.         strcat(fullnam,filenam);
  474.         if (f = fopen(fullnam,"r")) return f;
  475.     }
  476.     }
  477.     return NULL;
  478. }
  479.  
  480. /*************************************************************************/
  481.  
  482. void echo(c)
  483. char c;
  484. {
  485.     static int col = 11;
  486.     int i;
  487.  
  488.     echochar(c);
  489.     if (c == '\n') col = 11;
  490.     else if (++col > maxcol) {
  491.     col = 11;  ++paglin;
  492.     echochar('\n');
  493.     for (i = 1;  i <= 11;  ++i) echochar(' ');
  494.     }
  495. }
  496.  
  497. void echochar(c)
  498. char c;
  499. {
  500.     if (o_flg) {
  501.     if (fprintf(f_lst_fil,"%c",c) == ERROR) lst_err();
  502.     }
  503.     else printf("%c",c);
  504. }
  505.  
  506. /*************************************************************************/
  507.  
  508. int get_token(infile,g_token,g_toklen,g_eoflg,g_flg)
  509. FILE *infile;
  510. char *g_token;
  511. int *g_toklen, *g_eoflg, g_flg;
  512. {
  513.  
  514. /*
  515.     'getoken' returns the next valid identifier or
  516.     reserved word from a given file along with the
  517.     character length of the token and an end-of-file
  518.     indicator
  519. */
  520.  
  521.     int c;
  522.     char *h_token, tmpchr;
  523.  
  524.     h_token = g_token;
  525.  
  526.     gtk:
  527.     *g_toklen = 0;  g_token = h_token;
  528.  
  529. /*
  530.     Scan and discard any characters until an alphabetic or
  531.     '_' (underscore) character is encountered or an end-of-file
  532.     condition occurs
  533. */
  534.  
  535.     while ((!isalpha(*g_token = rdchr(infile,g_eoflg,g_flg)))
  536.     && !*g_eoflg && *g_token != '_' && *g_token != '0'
  537.     && *g_token != '#');
  538.  
  539.     if (*g_eoflg) return FALSE;
  540.     *g_toklen += 1;
  541.  
  542. /*
  543.     Scan and collect identified alpanumeric token until
  544.     a non-alphanumeric character is encountered or and
  545.     end-of-file condition occurs
  546. */
  547.  
  548.     if (g_flg) tmpchr = '.';
  549.     else tmpchr = '_';
  550.     while ((isalpha(c = rdchr(infile,g_eoflg,g_flg))
  551.     || isdigit(c) || c == '_' || c == tmpchr) && !*g_eoflg) {
  552.     if (*g_toklen < MAX_LEN) { *++g_token = c;  ++*g_toklen; }
  553.     }
  554.  
  555. /*
  556.     Check to see if a numeric hex or octal constant has
  557.     been encountered ... if so dump it and try again
  558. */
  559.  
  560.     if (*h_token == '0') goto gtk;
  561.  
  562. /*
  563.     Tack a NUL character onto the end of the token
  564. */
  565.  
  566.     *++g_token = '\0';
  567.  
  568. /*
  569.     Screen out all #token strings except #include
  570. */
  571.  
  572.     if (*h_token == '#' && strcmp(h_token,"#include")) goto gtk;
  573.  
  574.     return(TRUE);
  575. }
  576.  
  577. /*************************************************************************/
  578.  
  579. int fil_chr(infile,f_eof)
  580. FILE *infile;
  581. int *f_eof;
  582. {
  583.     int fc;
  584.  
  585.     if ((fc = getc(infile)) == EOF) {
  586.     if (ferror (infile)) {
  587.         printf("\nERROR: Error while processing input file - %s\n",
  588.         act_fil);
  589.         exit(0);
  590.     }
  591.     else if (feof(infile)) {
  592.         *f_eof = TRUE;  fc = NULL;
  593.     }
  594.     }
  595.     return fc;
  596. }
  597.  
  598. /*************************************************************************/
  599.  
  600. int rdchr(infile,r_eoflg,rd_flg)
  601. FILE *infile;
  602. int *r_eoflg, rd_flg;
  603. {
  604.  
  605. /*
  606.     'rdchr' returns the next valid character in a file
  607.     and an end-of-file indicator. A valid character is
  608.     defined as any which does not appear in either a
  609.     commented or a quoted string ... 'rdchr' will correctly
  610.     handle comment tokens which appear within a quoted
  611.     string
  612. */
  613.  
  614.     int c;
  615.     int q_flg;        /* double quoted string flag */
  616.     int q1_flg;        /* single quoted string flag */
  617.     int cs_flg;        /* comment start flag */
  618.     int ce_flg;        /* comment end flag */
  619.     int c_cnt;        /* comment nesting level */
  620.     int t_flg;        /* transparency flag */
  621.  
  622.     q_flg = q1_flg = cs_flg = ce_flg = t_flg = FALSE;
  623.     c_cnt  = 0;
  624.  
  625.     rch:
  626.  
  627. /*
  628.     Fetch character from file
  629. */
  630.  
  631.     c = fil_chr(infile,r_eoflg);
  632.     if (*r_eoflg) return c;       /* EOF encountered */
  633.     if (c == '\n') nl();
  634.     else echo(c);
  635.  
  636.     if (rd_flg) return c;
  637.  
  638.     if (t_flg) { t_flg = !t_flg;  goto rch; }
  639.     if (c == '\\') { t_flg = TRUE;  goto rch; }
  640.  
  641. /*
  642.     If the character is not part of a quoted string
  643.     check for and process commented strings...
  644.     nested comments are handled correctly but unbalanced
  645.     comments are not ... the assumption is made that
  646.     the syntax of the program being xref'd is correct.
  647.   NOTE: Now nested comment support is optional. The source for XC
  648.     formerly contained an un-matched begin comment. If nested
  649.     comment support is used to cross-reference the xc source
  650.     the program produces an incorrect cross-reference, yet
  651.     the Microsoft compiler compiles it correctly, as it
  652.     should. Nested comments are a non-standard extension to
  653.     the language. Therefore, nested comment support has been made
  654.     optional.
  655.     See usage information in comment at top of file.
  656. */
  657.  
  658.     if (!q_flg && !q1_flg) {
  659.     if (c == '*' && c_cnt && !cs_flg) { ce_flg = TRUE;  goto rch; }
  660.     if (c == '/' && ce_flg) {
  661.         c_cnt -= 1;     ce_flg = FALSE;  goto rch;
  662.     }
  663.     ce_flg = FALSE;
  664.     if (c == '/') { cs_flg = TRUE;    goto rch; }
  665.     if (c == '*' && cs_flg) {
  666.         c_cnt = (n_flg) ? TRUE : c_cnt + 1; /* optional nested comments */
  667.         cs_flg = FALSE;  goto rch;
  668.     }
  669.     cs_flg = FALSE;
  670.     if (c_cnt) goto rch;
  671.     }
  672.  
  673. /*
  674.     Check for and process quoted strings
  675. */
  676.  
  677.     if (c == '"' && !q1_flg) {
  678.     q_flg =     !q_flg;    /* toggle quote flag */
  679.     goto rch;
  680.     }
  681.     if (q_flg) goto rch;
  682.  
  683.     if (c == '\'') {
  684.     q1_flg = !q1_flg;    /* toggle quote flag */
  685.     goto rch;
  686.     }
  687.     if (q1_flg) goto rch;
  688.  
  689. /*
  690.     Valid character ... return to caller
  691. */
  692.  
  693.     return c;
  694. }
  695.  
  696. /*************************************************************************/
  697.  
  698. int chk_token(c_token)
  699. char *c_token;
  700. {
  701.     char u_token[MAX_LEN];
  702.     int i;
  703.  
  704.     if (r_flg) return TRUE;
  705.     i = 0;
  706.     do {
  707.     u_token[i] = toupper(c_token[i]);
  708.     } while (c_token[i++] != NULL);
  709.  
  710. /*
  711.  *  Support for the Microsoft extended keywords.
  712.  */
  713.  
  714.     if (m_flg) {
  715.     switch (u_token[0]) {
  716.         case 'C':
  717.         if (!strcmp(u_token,"CDECL")) return FALSE;
  718.         break;
  719.         case 'F':
  720.         if (!strcmp(u_token,"FORTRAN")) return FALSE;
  721.         if (!strcmp(u_token,"FAR")) return FALSE;
  722.         break;
  723.         case 'H':
  724.         if (!strcmp(u_token,"HUGE")) return FALSE;
  725.         break;
  726.         case 'N':
  727.         if (!strcmp(u_token,"NEAR")) return FALSE;
  728.         break;
  729.         case 'P':
  730.         if (!strcmp(u_token,"PASCAL")) return FALSE;
  731.         break;
  732.         case '_':
  733.         if (!strcmp(u_token,"_CDECL")) return FALSE;
  734.         if (!strcmp(u_token,"_FAR")) return FALSE;
  735.         break;
  736.     }
  737.     }
  738.  
  739. /*
  740.  *  Standard keyword support.
  741.  */
  742.  
  743.     switch (u_token[0]) {
  744.     case 'A':
  745.         if (!strcmp(u_token,"AUTO")) return FALSE;
  746.         break;
  747.     case 'B':
  748.         if (!strcmp(u_token,"BREAK")) return FALSE;
  749.         break;
  750.     case 'C':
  751.         if (!strcmp(u_token,"CHAR")) return     FALSE;
  752.         if (!strcmp(u_token,"CONTINUE")) return  FALSE;
  753.         if (!strcmp(u_token,"CONST")) return  FALSE;
  754.         if (!strcmp(u_token,"CASE")) return     FALSE;
  755.         break;
  756.     case 'D':
  757.         if (!strcmp(u_token,"DOUBLE")) return FALSE;
  758.         if (!strcmp(u_token,"DO")) return FALSE;
  759.         if (!strcmp(u_token,"DEFAULT")) return FALSE;
  760.         break;
  761.     case 'E':
  762.         if (!strcmp(u_token,"EXTERN")) return FALSE;
  763.         if (!strcmp(u_token,"ELSE")) return FALSE;
  764.         if (!strcmp(u_token,"ENUM")) return FALSE;
  765.         break;
  766.     case 'F':
  767.         if (!strcmp(u_token,"FLOAT")) return FALSE;
  768.         if (!strcmp(u_token,"FOR")) return FALSE;
  769.         break;
  770.     case 'G':
  771.         if (!strcmp(u_token,"GOTO")) return FALSE;
  772.         break;
  773.     case 'I':
  774.         if (!strcmp(u_token,"INT")) return FALSE;
  775.         if (!strcmp(u_token,"IF")) return FALSE;
  776.         break;
  777.     case 'L':
  778.         if (!strcmp(u_token,"LONG")) return FALSE;
  779.         break;
  780.     case 'R':
  781.         if (!strcmp(u_token,"RETURN")) return FALSE;
  782.         if (!strcmp(u_token,"REGISTER")) return FALSE;
  783.         break;
  784.     case 'S':
  785.         if (!strcmp(u_token,"STRUCT")) return FALSE;
  786.         if (!strcmp(u_token,"SHORT")) return FALSE;
  787.         if (!strcmp(u_token,"STATIC")) return FALSE;
  788.         if (!strcmp(u_token,"SIZEOF")) return FALSE;
  789.         if (!strcmp(u_token,"SWITCH")) return FALSE;
  790.         if (!strcmp(u_token,"SIGNED")) return FALSE;
  791.         break;
  792.     case 'T':
  793.         if (!strcmp(u_token,"TYPEDEF")) return FALSE;
  794.         break;
  795.     case 'U':
  796.         if (!strcmp(u_token,"UNION")) return FALSE;
  797.         if (!strcmp(u_token,"UNSIGNED")) return FALSE;
  798.         break;
  799.     case 'V':
  800.         if (!strcmp(u_token,"VOID")) return FALSE;
  801.         if (!strcmp(u_token,"VOLATILE")) return FALSE;
  802.         break;
  803.     case 'W':
  804.         if (!strcmp(u_token,"WHILE")) return FALSE;
  805.         break;
  806.     }
  807.     return TRUE;
  808. }
  809.  
  810. /*************************************************************************/
  811.  
  812. /*
  813.    Install parsed token and line reference in linked structure
  814. */
  815.  
  816. void put_token(p_token,p_ref)
  817. char *p_token;
  818. int p_ref;
  819. {
  820.     int hsh_index, i, j, d, found;
  821.     struct id_blk *idptr;
  822.     struct id_blk *alloc_id();
  823.     struct rf_blk *alloc_rf(), *add_rf();
  824.  
  825.     if (l_flg) return;
  826.     j=0;
  827.     for (i = 0;     p_token[i] != NULL;  ++i) {    /* Hashing algorithm is far */
  828.     j = j * 10 + p_token[i];        /* from optimal but is        */
  829.     }                        /* adequate for a memory-   */
  830.     hsh_index = abs(j) % MAX_WRD;        /* bound index vector!        */
  831.     found = FALSE;  d = 1;
  832.     do {
  833.     idptr = id_vector[hsh_index];
  834.     if (idptr == NULL) {
  835.         id_cnt++;
  836.         idptr = id_vector[hsh_index] = alloc_id(p_token);
  837.         chain_alpha(idptr,p_token);
  838.         idptr->top_lnk = idptr->lst_lnk = alloc_rf(p_ref);
  839.         found = TRUE;
  840.     }
  841.     else if (!strcmp(p_token,idptr->id_name)) {
  842.         idptr->lst_lnk = add_rf(idptr->lst_lnk,p_ref);
  843.         found = TRUE;
  844.     }
  845.     else {
  846.         hsh_index += d;  d += 2;  rhsh_cnt++;
  847.         if (hsh_index >= MAX_WRD) hsh_index -= MAX_WRD;
  848.         if (d == MAX_WRD) {
  849.         printf("\nERROR: Symbol table overflow\n");
  850.         exit(0);
  851.         }
  852.     }
  853.     } while (!found);
  854. }
  855.  
  856. /*************************************************************************/
  857.  
  858. void chain_alpha(ca_ptr,ca_token)
  859. struct id_blk *ca_ptr;
  860. char  *ca_token;
  861. {
  862.     char c;
  863.     struct id_blk *cur_ptr;
  864.     struct id_blk *lst_ptr;
  865.  
  866.     if ((c = ca_token[0]) == '_') c = 0;
  867.     else if (isupper(c)) c = 1 + ((c - 'A') * 2);
  868.     else c = 2 + ((c - 'a') * 2);
  869.  
  870.     if (!alpha_vector[c].alpha_top) {
  871.     alpha_vector[c].alpha_top = alpha_vector[c].alpha_lst = ca_ptr;
  872.     ca_ptr->alpha_lnk = NULL;  return;
  873.     }
  874.  
  875. /*
  876.     check to see if new id_blk should be inserted between
  877.     the alpha_vector header block and the first id_blk in
  878.     the current alpha chain
  879. */
  880.  
  881.     if (strcmp(alpha_vector[c].alpha_top->id_name,ca_token) > 0) {
  882.     ca_ptr->alpha_lnk=alpha_vector[c].alpha_top;
  883.     alpha_vector[c].alpha_top=ca_ptr;  return;
  884.     }
  885.     if (strcmp(alpha_vector[c].alpha_lst->id_name,ca_token) < 0) {
  886.     alpha_vector[c].alpha_lst->alpha_lnk = ca_ptr;
  887.     ca_ptr->alpha_lnk = NULL;  alpha_vector[c].alpha_lst=ca_ptr;  return;
  888.     }
  889.     cur_ptr = alpha_vector[c].alpha_top;
  890.     while (strcmp(cur_ptr->id_name,ca_token) < 0) {
  891.     lst_ptr = cur_ptr;  cur_ptr = lst_ptr->alpha_lnk;
  892.     }
  893.     lst_ptr->alpha_lnk = ca_ptr;  ca_ptr->alpha_lnk = cur_ptr;    return;
  894. }
  895.  
  896. /*************************************************************************/
  897.  
  898. struct id_blk *alloc_id(aid_token)
  899. char *aid_token;
  900. {
  901.     int ai;
  902.     struct id_blk *aid_ptr;
  903.  
  904.     if (!(aid_ptr = (struct id_blk *)malloc(sizeof(struct id_blk)))) {
  905.     printf("\nERROR: Unable to allocate identifier block\n");
  906.     exit(0);
  907.     }
  908.     ai = 0;
  909.     do
  910.     aid_ptr->id_name[ai] = aid_token[ai];
  911.     while (aid_token[ai++] != NULL);
  912.     return aid_ptr;
  913. }
  914.  
  915. /*************************************************************************/
  916.  
  917. struct rf_blk *alloc_rf(arf_ref)
  918. int arf_ref;
  919. {
  920.     int ri;
  921.     struct rf_blk *arf_ptr;
  922.  
  923.     if (!(arf_ptr = (struct rf_blk *)malloc(sizeof(struct rf_blk)))) {
  924.     printf("\nERROR: Unable to allocate reference block\n");
  925.     exit(0);
  926.     }
  927.     arf_ptr->ref_item[0] = arf_ref;  arf_ptr->ref_cnt.cnt = 1;
  928.     for (ri = 1;  ri < MAX_REF;     ++ri) arf_ptr->ref_item[ri] = NULL;
  929.     return arf_ptr;
  930. }
  931.  
  932. /*************************************************************************/
  933.  
  934. struct rf_blk *add_rf(adr_ptr,adr_ref)
  935. struct rf_blk *adr_ptr;
  936. int adr_ref;
  937. {
  938.     struct rf_blk *tmp_ptr;
  939.  
  940.     tmp_ptr = adr_ptr;
  941.     if (adr_ptr->ref_cnt.cnt == MAX_REF) {
  942.     tmp_ptr = alloc_rf(adr_ref);
  943.     adr_ptr->ref_cnt.pnext = tmp_ptr;
  944.     }
  945.     else adr_ptr->ref_item[adr_ptr->ref_cnt.cnt++] = adr_ref;
  946.     return tmp_ptr;
  947. }
  948.  
  949. /*************************************************************************/
  950.  
  951. void prnt_tbl()
  952. {
  953.     int prf_cnt, pti, pref, lin_cnt;
  954.     struct id_blk *pid_ptr;
  955.     struct rf_blk *ptb_ptr;
  956.  
  957.     prt_ref = TRUE;  prt_hdr();
  958.     for (pti = 0;  pti < MAX_ALPHA;  ++pti) {
  959.     if (pid_ptr = alpha_vector[pti].alpha_top) {
  960.         do {
  961.         if (o_flg) {
  962.             if (fprintf(f_lst_fil,"%-20.19s: ",pid_ptr->id_name)
  963.             == ERROR) lst_err();
  964.         }
  965.         else printf("%-20.19s: ",pid_ptr->id_name);
  966.         ptb_ptr = pid_ptr->top_lnk;  lin_cnt = prf_cnt = 0;
  967.         do {
  968.             if (prf_cnt == MAX_REF) {
  969.             prf_cnt = 0;
  970.             ptb_ptr = (struct rf_blk *)ptb_ptr->ref_cnt.pnext;
  971.             }
  972.             if (ptb_ptr > (struct rf_blk *)MAX_REF) {
  973.             if (pref = ptb_ptr->ref_item[prf_cnt++]) {
  974.                 if (o_flg) {
  975.                 if (fprintf(f_lst_fil,"%4d ",pref) == ERROR)
  976.                     lst_err();
  977.                 }
  978.                 else printf("%4d ",pref);
  979.                 if (++lin_cnt == REFS_PER_LINE) {
  980.                 nl();
  981.                 if (o_flg) {
  982.                     if (fprintf(f_lst_fil,
  983.                     "                      ") == ERROR)
  984.                     lst_err();
  985.                 }
  986.                 else printf("                      ");
  987.                 lin_cnt=0;
  988.                 }
  989.             }
  990.             }
  991.             else pref=0;
  992.         } while (pref);
  993.         nl();
  994.         } while ((pid_ptr=pid_ptr->alpha_lnk) != NULL);
  995.     }
  996.     }
  997. /*for*/
  998.  
  999.     echo( '\n' );
  1000. }
  1001.  
  1002. /*************************************************************************/
  1003.  
  1004. void prt_hdr()
  1005. {
  1006.     if (pagno++) { echo('\n'); echo(FF); }
  1007.     if (o_flg) {
  1008.     if (fprintf(f_lst_fil,
  1009.         "XC ... 'C' Concordance Utility   %-20s       Page %d",
  1010.         gbl_fil,pagno) == ERROR) lst_err();
  1011.     }
  1012.     else printf("XC ... 'C' Concordance Utility   %-20s       Page %d",
  1013.     gbl_fil,pagno);
  1014.     echo('\n');     paglin = 3;  nl();
  1015. }
  1016.  
  1017. /*************************************************************************/
  1018.  
  1019. void nl()
  1020. {
  1021.     echo('\n');
  1022.     if (++paglin >= LINES_PER_PAGE) prt_hdr();
  1023.     else if (!prt_ref) {
  1024.     if (o_flg) {
  1025.         if (fprintf(f_lst_fil,"%-4d %4d: ",++linum,++edtnum) == ERROR)
  1026.         lst_err();
  1027.     }
  1028.     else printf("%-4d %4d: ",++linum,++edtnum);
  1029.     if (o_flg) {
  1030.         if (linum % 60 == 1) printf("\n<%d> ",linum);
  1031.         else {
  1032.         printf(".");
  1033.         fflush(stdout);
  1034.         }
  1035.     }
  1036.     }
  1037.     return;
  1038. }
  1039.  
  1040.